package cmd;

import haxe.ds.List;
import tree.LeafKind;
import tree.Leaf;

class Cmd extends Leaf{
    //可能考虑取消
    private var doList : List<Void->Void>;
    private var undoList : List<Void->Void>;

    public function new() {
        super();
        doList = new List<Void->Void>();
        undoList = new List<Void->Void>();
    }

    public function AddCallback_Do(e) {
        if(e != null){
            doList.add(e);
        }
    }

    public function AddCallback_Undo(e) {
        if(e != null){
            undoList.add(e);
        }
    }

    public function RemoveCallback_Do(e) {
        if(e != null){
            doList.remove(e);
        }
    }

    public function RemoveCallback_Undo(e) {
        if(e != null){
            undoList.remove(e);
        }
    }

    private function InvokeCallback_Do() {
        for (cb in doList){
            cb();
        }
    }

    private function InvokeCallback_UnDo() {
        for (cb in undoList){
            cb();
        }
    }

    public override function OnEnter(last:LeafKind) {
        super.OnEnter(last);
        Do();
        InvokeCallback_Do();
    }

    public function Do() {
        
    }

    public override function OnExit(next:LeafKind) {
        super.OnExit(next);
        Undo();
        InvokeCallback_UnDo();
    }

    public function Undo() {
        
    }

}